//
// Created by hsj on 2022/1/11.
//
#include <iostream>
#include <new>
#include "t_new_operator.h"

//测试new操作符创建
void test_new_operator(){
    using namespace std;

    int *pi = new int(18);
    //C++11
    double *pd = new double {18.0};

    struct hsj{
        double x;
        double y;
        const char *name;
    };
    hsj *p_hsj = new hsj{18.0,29.0,"hsj is smart!"};
    cout<<"test_new_operator hsj.name:"<<p_hsj->name<<endl;


    //new
    char buffer1[50];
    char buffer2[500];

    hsj *p_hsj_1,*p_hsj_2;
    int *p_int_1,*p_int_2;

    p_hsj_1 = new hsj;//heap
    p_int_1 = new int[20];//heap

    p_hsj_2 = new (buffer1) hsj;// buffer1
    p_int_2 = new (buffer2) int[20];//buffer2
    p_int_2 = new (buffer2 + 20 * sizeof(int)) int[20];//buffer2 offset 20*4 bytes（假设int大小4字节）

    delete p_hsj_1;
    delete [] p_int_1;

//    delete p_hsj_2;//do not work!delete只负责heap堆内存
//    delete [] p_int_2;//do not work!delete只负责heap堆内存

}
